<?php

declare(strict_types=1);

namespace app\common\business;

use app\common\lib\ClassArr;
use app\common\lib\Num;
use app\common\lib\sms\AliSms;

class Sms
{

    public static function sendCode(string $phoneNumber, int $len, string $type = "ali"): bool
    {
        // 生成短信验证码
        //$code = rand(100000, 999999);
        $code = Num::getCode($len);
        //$sms = AliSms::sendCode($phoneNumber, $code);

        //工厂模式
        /*$type = ucfirst($type);//首字母大小
        $class = "app\common\lib\sms\\" . $type . "Sms";
        $sms = $class::sendCode($phoneNumber, $code);*/ //此处编辑器报错，测试时需要注意

        //上述工厂模式还可利用反射机制处理优化
        $classStat = ClassArr::smsClassStat();
        $classObj = ClassArr::initClass($type, $classStat);
        //$sms = $classObj::sendCode($phoneNumber, $code);
        $sms = true;
        if ($sms) {
            //需要把短信验证码记录到redis 并给一分钟失效时间
            //1、php安装redis扩展
            //2、启动redis服务
            cache(config('redis.code_pre') . $phoneNumber, $code, config('redis.code_expire'));
        }

        return $sms;
    }
}
